<template>
  <div>
    <Editor style="width: 900px" v-model="contentValue" :init="initOption" />
  </div>
</template>

<script setup lang="ts">
import Editor from "@tinymce/tinymce-vue";
import tinymce from "tinymce/tinymce";
import { upload } from "@/request/http";
import { computed } from "vue";
let props = defineProps({
  //用于自定义v-model的value，这个名称是固定的
  modelValue: {
    type: String,
    default: "",
  },
});

const emits = defineEmits(["update:modelValue"]); //更新modelValue事件
let contentValue = computed({
  get() {
    //取值
    return props.modelValue;
  },
  set(value) {
    //赋值
    emits("update:modelValue", value);
  },
});

let initOption = {
  base_url: "/tinymce", //【必要参数】指定public的目录
  language: "zh_CN",
  plugins:
    " preview searchreplace autolink directionality visualblocks visualchars fullscreen image link media template code codesample table charmap pagebreak nonbreaking anchor insertdatetime advlist lists wordcount autosave emoticons", //插件配置 axupimgs indent2em,  //插件配置
  toolbar:
    "fullscreen undo redo restoredraft  |  table image axupimgs media  charmap hr pagebreak insertdatetime print preview | cut copy paste  | forecolor backcolor bold italic underline strikethrough link anchor | styleselect formatselect fontselect fontsizeselect ", //工具栏配置，设为false则隐藏
  toolbar_mode: "sliding", //工具栏移除模式 floating / sliding / scrolling / wrap
  fontsize_formats: "12px 14px 16px 18px 20px 22px 24px 28px 32px 36px 48px 56px 72px", //字体大小
  font_formats:
    "微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方=PingFang SC,Microsoft YaHei,sans-serif;宋体=simsun,serif;仿宋体=FangSong,serif;黑体=SimHei,sans-serif;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;", //字体样式
  lineheight_formats: "0.5 0.8 1 1.2 1.5 1.75 2 2.5 3 4 5", //行高配置，也可配置成"12px 14px 16px 20px"这种形式
  height: 400, //注：引入autoresize插件时，此属性失效
  placeholder: "在这里输入文字",
  branding: false, //tiny技术支持信息是否显示
  resize: false, //编辑器宽高是否可变，false-否,true-高可变，'both'-宽高均可，注意引号
  elementpath: false, //元素路径是否显示
  content_style: "img {max-width:100%;}", //直接自定义可编辑区域的css样式
  paste_data_images: true, //图片是否可粘贴
  //自定义图片上传逻辑
  images_upload_handler: (blobInfo: any, success: any, failure: any) => {
    let formData = new FormData();
    formData.append("file", blobInfo.blob());
    upload(formData).then((res: any) => {
      if (200 == res.errno) {
        success(res.url);
      } else {
        failure("HTTP Error: 上传失败");
      }
    });
  },
};
</script>

<style></style>
